// pages/signlist/signlist.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    label: [],
    active: 0,
    ids: [],
    idArr: [],
    chan: []
  },
  getsignlist() {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let arys = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
    };
    let _token = axios.encryption(arys);
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/signlist",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        res.data.result.forEach((element) => {
          for (let i = 0; i < this.data.idArr.length; i++) {
            if (this.data.idArr[i] == element.id) {
              element.color = "#FE804D"
            }
          }
        })
        this.setData({
          label: res.data.result
        })
        // console.log(this.data.label);
      }
    })
  },
  // 选择标签
  setTab(e) {
    let label = this.data.label
    let index = e.currentTarget.dataset.index
    let id = e.currentTarget.dataset.id
    let chan = this.data.chan
    if (label[index].color == "#FE804D") {
      label[index].color = "#000000"

      var row = chan.map(item => item).indexOf(id)
      chan.splice(row, 1);
    } else {
      if (chan.length == 5) {
        wx.showToast({
          title: '只能选五个',
          icon: "none"
        })
        return
      }
      label[index].color = "#FE804D"
      chan.push(label[index].id)
      label[index].color = '#FE804D'
    }
    this.setData({
      label,
      chan
    })
    // console.log(this.data.chan);
  },
  //保存
  preservation() {
    // var reg = new RegExp('"', "g");
    // var newStr = this.data.chan.toString().replace(reg, "");
    // console.log(newStr);
    let signIds = this.data.chan.toString();
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&signIds=" +
      signIds +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    console.log(_token);
    let params = {
      signIds: signIds,
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/editsign",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        wx.showToast({
          title: res.data.text,
          icon: "none"
        })
      } else {
        wx.showToast({
          title: res.data.text,
          icon: "none"
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    let arr = [];
    arr = options.id.split(",");
    let idArr = [];
    arr.forEach(e => {
      idArr.push(Number(e))
    })
    // console.log(idArr);
    this.setData({
      idArr: idArr,
      chan: idArr
    })
    this.getsignlist()


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})